<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

require_once(APPPATH.'controllers/admin/applications.php');
class Add_new extends Applications
{
	public function __construct()
	{
		parent::__construct();

		/**
		 * @description check the session variables
		 */
		$this->M_login->session_check();

		/**
		 * @description the URL
		 */
		$this->bz_segment['END'] = 5;
		$this->bz_segment['URL'] = $this->M_functions->segment_create();
		$this->bz_segment['SITE_URL'] = site_url($this->bz_segment['URL']);

		/**
		 * @description access rights
		 */
		$this->access_rights = $this->M_menu->valid_check('db_default', $this->session->userdata('session_user_id'));
		$this->access_rights = array
			(
				'ADD' => in_array('add', $this->access_rights) ? TRUE : FALSE
				, 'EDIT' => in_array('edit', $this->access_rights) ? TRUE : FALSE
				, 'DELETE' => in_array('delete', $this->access_rights) ? TRUE : FALSE
			);

		/**
		 * @description initialize variables for database
		 */
		$this->tbl_attr = array
			(
				'TABLE' => array
					(
						'NAME' => 'hris_employee_data'
						, 'NAME_ALIAS' => 'hris_employee_data a'
						, 'PRIMARY' => array
							(
								'SHOW' => FALSE
								, 'DATA' => array('id')
							)
					)
				, 'VIEW' => array
					(
						'HEADING' => array
							(
								$this->lang->line('hris_code')
								, $this->lang->line('hris_name')
								, $this->lang->line('hris_type')
								, $this->lang->line('hris_company_level')
								, $this->lang->line('hris_company_owner_name')
								, $this->lang->line('hris_phone')
								, $this->lang->line('hris_fax')
								, $this->lang->line('hris_email')
								, $this->access_rights['DELETE'] ? '&nbsp;' : NULL
							)
						, 'FIELDS' => 'id, code, name, type, (select name from bz_hris_master_data_language b where a.company_level__id = b.data__id and b.language__id = "'.$this->session->userdata('session_user_language_id').'") as company_level_name, owner_name, phone, fax, email'
						, 'FIELDS_EDIT' => 'name'
						, 'FIELDS_ORDER' => 'name'
						, 'FIELDS_PARENT_NAME' => 'parent_id'
						, 'TABLE' => array
							(
								'WHERE' => array()
							)
					)
				, 'FORM' => array
					(
						'FIELDS' => '*'
						, 'FIELDS_ORDER' => ''
						, 'COLUMN' => 4
						, 'COLUMN_JOIN' => 2
					)
				, 'URL' => array
					(
						'FORM' => $this->bz_segment['SITE_URL'].'/form'
						, 'VIEW' => $this->bz_segment['SITE_URL'].'/view'
					)
			);

		/**
		 * @description template lite variables
		 */
		$this->bz_tlite_var['CACHE_PATH'] = $this->bz_tlite_var['SESSION']['USER_ID'].'|'.str_replace('/', '|', $this->bz_segment['URL']);
		$this->bz_tlite_var['CACHE'] = array
			(
				'PUBLIC' => $this->bz_tlite_var['SESSION']['USER_ID']
				, 'PRIVATE' => array
					(
						'GLOBAL' => $this->bz_tlite_var['CACHE_PATH']
						, 'VIEW' => $this->bz_tlite_var['CACHE_PATH'].'|view'
						, 'FORM' => $this->bz_tlite_var['CACHE_PATH'].'|form'
					)
			);
	}


	/**
	 * @function index()
	 * @description main controller
	 */
	public function index()
	{
		/**
		 * @description choose which controller to show
		 */
		redirect($this->tbl_attr['URL']['FORM']);
	}


	/**
	 * @function form()
	 * @description form for data entry
	 */
	public function form($prm_key = '')
	{
		if ($this->web_data['BISKITZ']['HEADER']['BROWSER_SUPPORTED'] == '')
		{
			/**
			 * @description if the user has ADD or EDIT access, show the form!
			 */
			if ($this->access_rights['ADD'] OR ($this->access_rights['EDIT'] && trim($prm_key) != ''))
			{
				/**
				 * @description if there isn't any css/ajax cache, build it! else show cache!
				 */
				if ( ! $this->bz_tlite->is_cached($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']))
				{
					/**
					 * @section begin page init
					 */
						/**
						 * @description show the menu
						 */
						$menu = $this->M_menu->shows();
						$this->web_data['JAVASCRIPT_MENU'] = $menu['JAVASCRIPT_MENU'];
						$this->web_data['PAGE']['MENU'] = $menu['PAGE']['MENU'];

						/**
						 * @description init the css and the ajax needed
						 */
						$css_files = array
							(
								array('layout' => 'screen')
								, array('tipsy/tipsy' => 'screen')
								, array('validate/validate' => 'screen')
								, array('form_icon_bar' => 'screen')
								, array('nanoscroller/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('shortcut_bar' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('dynDateTime/dynDateTime' => 'screen')
								, array('ddm/multi-ddm' => 'screen')
							);
						$js_files = array
							(
								'js/plugins/ddm' => array('jquery.multi-ddm.pack')
								, 'js/plugins/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? array('jquery.nanoscroller.min', 'overthrow.min') : NULL
								, 'js/plugins/validate' => array('jquery.validate.pack', 'jquery.metadata')
								, 'js/plugins/date' => array('date')
								, 'js/plugins/tipsy' => array('jquery.tipsy.pack')
								, 'js/plugins/dynDateTime' => array('jquery.dynDateTime.pack')
								, 'js/plugins/dynDateTime/lang/pack' => array('calendar-'.$this->session->userdata('session_user_language_abbr'))
								, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_FUNCTIONS_PATH'] => array('common', 'form', 'error', 'datetime_picker')
								, 'themes/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN'].'/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_PATH'] => array('common')
							);


						/**
						 * @section begin form initialize
						 */
							/**
							 * @description load the models
							 */
							$this->load->model('admin/hris/HRIS_M_live_data');


							/**
							 * @description initialize the form elements
							 */
								/**
								 * @description Employee Company
								 */
								$form_content_join[] = array
									(
										array('data' => '<b class="big highlight">'.$this->lang->line('hris_company').'</b>', 'class' => 'highlight')
										, array('data' => '<b class="big highlight">&nbsp;</b>', 'class' => 'highlight', 'colspan' => 3)

										, '<b>'.$this->lang->line('hris_company_name').'</b>'
										, array
											(
												'data' => form_dropdown
													(
														'txtinput[0]'
														, $this->HRIS_M_live_data->combo_fill('db_default', 'company_profile', '', 'RECURSIVE', array(), FALSE)
														, ''
														, 'id="txt0" class="{validate: {required: true}}"'
													)
												, 'colspan' => 3
											)
									);

								$form_content[] = array
									(
										'<b>'.$this->lang->line('hris_job_status').'</b>'
										, form_dropdown
											(
												'txtinput[1]'
												, $this->HRIS_M_live_data->combo_fill('db_default', 27)
												, ''
												, 'id="txt1" class="{validate: {required: true}}"'
											)

										, '<b class="hidden">'.$this->lang->line('hris_period').'</b>'
										, form_dropdown
											(
												'txtinput[2]'
												, $this->HRIS_M_live_data->combo_fill('db_default', 43)
												, ''
												, 'id="txt2" class="{validate: {required: true}} hidden"'
											)

										, '<b>'.$this->lang->line('hris_employee_join_date_start').'</b>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[3]', 'id' => 'txt3'
													, 'size' => '11', 'maxlength' => '10'
													, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
												)
												, date('Y-m-d')
												, 'onBlur="javascript: calendar.hide(); $(\'#txt2\').change();"'
											)

										, '<b class="hidden">'.$this->lang->line('hris_employee_join_date_end').'</b>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[4]', 'id' => 'txt4'
													, 'size' => '11', 'maxlength' => '10'
													, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} do_not_change read_only hidden'
												)
												, date('Y-m-d')
											)
									);

								$form_content_join[] = array
									(
										'<b>'.$this->lang->line('hris_employee_effective_date').'</b>'
										, array
											(
												'data' => form_input
													(
														array
														(
															'name' => 'txtinput[5]', 'id' => 'txt5'
															, 'size' => '11', 'maxlength' => '10'
															, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
														)
														, date('Y-m-d')
														, 'onBlur="javascript: calendar.hide();"'
													)
												, 'colspan' => 3
											)

										, '<b>'.$this->lang->line('hris_schema_department').'</b>'
										, array
											(
												'data' => form_dropdown
													(
														'txtinput[34]'
														, array('' => $this->lang->line('hris_choose'))
														, ''
														, 'id="txt34"'
													)
												, 'colspan' => 3
											)

										, '<b>'.$this->lang->line('hris_schema_department_sub').'</b>'
										, array
											(
												'data' => form_dropdown
													(
														'txtinput[35]'
														, array('' => $this->lang->line('hris_choose'))
														, ''
														, 'id="txt35"'
													)
												, 'colspan' => 3
											)

										, '<b>'.$this->lang->line('hris_schema_division').'</b>'
										, array
											(
												'data' => form_dropdown
													(
														'txtinput[36]'
														, array('' => $this->lang->line('hris_choose'))
														, ''
														, 'id="txt36"'
													)
												, 'colspan' => 3
											)


										, '<b>'.$this->lang->line('hris_job_title').'</b>'
										, array
											(
												'data' => form_dropdown
													(
														'txtinput[6]'
														, array('' => $this->lang->line('hris_choose'))
														, ''
														, 'id="txt6" class="{validate: {required: true}}"'
													)
												, 'colspan' => 3
											)
									);

								$form_content[] = array
									(
										'<b>'.$this->lang->line('hris_schema_position').'</b>'
										, form_dropdown
											(
												'txtinput[7]'
												, array('' => $this->lang->line('hris_choose'))
												, ''
												, 'id="txt7" class="{validate: {required: true}}"'
											)

										, '<b>'.$this->lang->line('hris_position_grade').'</b>'
										, form_dropdown
											(
												'txtinput[8]'
												, array('' => $this->lang->line('hris_choose'))
												, ''
												, 'id="txt8" class="{validate: {required: true}}"'
											)

										, '<b>'.$this->lang->line('hris_work_location').'</b>'
										, form_dropdown
											(
												'txtinput[9]'
												, array('' => $this->lang->line('hris_choose'))
												, ''
												, 'id="txt9" class="{validate: {required: true}}"'
											)

										, '<b class="hidden">'.$this->lang->line('hris_instantion_institution').'</b>'
										, form_dropdown
											(
												'txtinput[10]'
												, array('' => $this->lang->line('hris_choose'))
												, ''
												, 'id="txt10" class="hidden"'
											)
									);

								$form_content_join[] = array
									(
										/**
										 * @description Employee Data
										 */
										array('data' => '<b class="big highlight">'.$this->lang->line('hris_employee_data').'</b>', 'class' => 'highlight')
										, array('data' => '<b class="big highlight">&nbsp;</b>', 'class' => 'highlight', 'colspan' => 3)

										, '<b>'.$this->lang->line('hris_employee_id').'</b><br /><small>'.$this->lang->line('fields_auto').'</small>'
										, array
											(
												'data' => form_input
													(
														array
														(
															'name' => 'txtinput[11]', 'id' => 'txt11'
															, 'size' => '22', 'maxlength' => '20'
															, 'readOnly' => 'readOnly' , 'class' => '{validate: {required: true}} read_only bold'
														)
													)
												, 'colspan' => 3
											)

										, '<b>'.$this->lang->line('hris_employee_number').'</b><br /><small>'.$this->lang->line('fields_auto').'</small>'
										, array
											(
												'data' => form_input
													(
														array
														(
															'name' => 'txtinput[12]', 'id' => 'txt12'
															, 'size' => '22', 'maxlength' => '20'
															, 'readOnly' => 'readOnly' , 'class' => '{validate: {required: true}} read_only bold'
														)
													)
												, 'colspan' => 3
											)

										, '<b>'.$this->lang->line('hris_name').'</b><br /><small>'.$this->lang->line('fields_required').'</small>'
										, array
											(
												'data' => form_input
													(
														array
														(
															'name' => 'txtinput[13]', 'id' => 'txt13'
															, 'size' => '20', 'maxlength' => '20'
															, 'class' => '{validate: {required: true}}'
														)
														, $this->lang->line('hris_employee_name_first')
													)
													. '&nbsp;'
													. form_input
														(
															array
															(
																'name' => 'txtinput[14]', 'id' => 'txt14'
																, 'size' => '20', 'maxlength' => '20'
															)
															, $this->lang->line('hris_employee_name_middle')
														)
													. '&nbsp;'
													. form_input
														(
															array
															(
																'name' => 'txtinput[15]', 'id' => 'txt15'
																, 'size' => '20', 'maxlength' => '20'
															)
															, $this->lang->line('hris_employee_name_last')
														)
												, 'colspan' => 3
											)
									);

								$form_content[] = array
									(
										'<b>'.$this->lang->line('hris_employee_name_official').'</b>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[16]', 'id' => 'txt16'
													, 'size' => '60', 'maxlength' => '60'
													, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} read_only'
												)
											)

										, '<b>'.$this->lang->line('hris_employee_name_nick').'</b>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[17]', 'id' => 'txt17'
													, 'size' => '20', 'maxlength' => '20'
												)
											)

										, '<b>'.$this->lang->line('hris_salutation').'</b>'
										, form_dropdown
											(
												'txtinput[18]'
												, $this->HRIS_M_live_data->combo_fill('db_default', 3)
												, ''
												, 'id="txt18"'
											)

										, '<b>'.$this->lang->line('hris_nationality').'</b>'
										, form_dropdown
											(
												'txtinput[19]'
												, $this->HRIS_M_live_data->combo_fill('db_default', 41)
												, ''
												, 'id="txt19" class="{validate: {required: true}}"'
											)

										, '<b>'.$this->lang->line('hris_employee_birth_place').'</b>'
										, form_dropdown
											(
												'txtinput[20]'
												, array('' => $this->lang->line('hris_choose'))
												, ''
												, 'id="txt20" class="{validate: {required: true}}"'
											)

										, '<b>'.$this->lang->line('hris_employee_birth_date').'</b>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[21]', 'id' => 'txt21'
													, 'size' => '11', 'maxlength' => '10'
													, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
												)
												, date('Y-m-d')
												, 'onBlur="javascript: calendar.hide();"'
											)

										, '<b>'.$this->lang->line('hris_gender').'</b>'
										, form_dropdown
											(
												'txtinput[22]'
												, $this->HRIS_M_live_data->combo_fill('db_default', 38, '', 'NORMAL', array('id !=' => 221))
												, ''
												, 'id="txt22" class="{validate: {required: true}}"'
											)

										, '<b>'.$this->lang->line('hris_religion').'</b>'
										, form_dropdown
											(
												'txtinput[23]'
												, $this->HRIS_M_live_data->combo_fill('db_default', 1)
												, ''
												, 'id="txt23" class="{validate: {required: true}}"'
											)
									);

								$form_content_join[] = array
									(
										'<b>'.$this->lang->line('hris_marital_status').'</b>'
										, array
											(
												'data' => form_dropdown
													(
														'txtinput[24]'
														, $this->HRIS_M_live_data->combo_fill('db_default', 2)
														, ''
														, 'id="txt24" class="{validate: {required: true}}"'
													)
												, 'colspan' => 3
											)
									);

								$form_content[] = array
									(
										'<b>'.$this->lang->line('hris_employee_marital_place').'</b>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[25]', 'id' => 'txt25'
													, 'size' => '30', 'maxlength' => '30'
												)
											)

										, '<b>'.$this->lang->line('hris_employee_marital_date').'</b>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[26]', 'id' => 'txt26'
													, 'size' => '11', 'maxlength' => '10'
													, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
												)
												, date('Y-m-d')
												, 'onBlur="javascript: calendar.hide();"'
											)

										, '<b>'.$this->lang->line('hris_employee_id_card_number').'</b><br /><small>'.$this->lang->line('fields_required').'</small>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[27]', 'id' => 'txt27'
													, 'size' => '30', 'maxlength' => '30'
													, 'class' => '{validate: {required: true}}'
												)
											)

										, '<b>'.$this->lang->line('hris_employee_id_card_expire').'</b>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[28]', 'id' => 'txt28'
													, 'size' => '11', 'maxlength' => '10'
													, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
												)
												, date('Y-m-d')
												, 'onBlur="javascript: calendar.hide();"'
											)

										, '<b>'.$this->lang->line('hris_employee_tax_id').'</b>'
										, form_input
											(
												array
												(
													'name' => 'txtinput[29]', 'id' => 'txt29'
													, 'size' => '30', 'maxlength' => '30'
												)
											)

										, '<b>'.$this->lang->line('hris_tax_type').'</b>'
										, form_dropdown
											(
												'txtinput[30]'
												, $this->HRIS_M_live_data->combo_fill('db_default', 11)
												, ''
												, 'id="txt30"'
											)
									);

								$form_content_join[] = array
									(
										'<b>'.$this->lang->line('hris_employee_finger').'</b><br /><small>'.$this->lang->line('fields_required').'</small>'
										, array
											(
												'data' => form_input
												(
													array
													(
														'name' => 'txtinput[31]', 'id' => 'txt31'
														, 'size' => '30', 'maxlength' => '30'
														, 'class' => '{validate: {required: true}}'
													)
												)
												, 'colspan' => 3
											)

										/**
										 * @description Employee Leave
										 */
										, array('data' => '<b class="big highlight">'.$this->lang->line('hris_attendance_leave_leave').'</b>', 'class' => 'highlight')
										, array('data' => '<b class="big highlight">&nbsp;</b>', 'class' => 'highlight', 'colspan' => 3)

										, array('data' => '<b>'.$this->lang->line('hris_attendance_leave_leave').'</b><br /><small>'.$this->lang->line('fields_choose_more').'</small>', 'valign' => 'top')
										, array
											(
												'data' => form_dropdown
													(
														'txtinput[32][]'
														, array()
														, array()
														, 'id="txt32" class="{validate: {required: true}}"'
															. ' multiple="multiple" size="10" style="width: 100%"'
													)
													. '<div align="right">'.anchor('#', $this->lang->line('hris_select_toggle'), array('class' => 'link_select_toggle')).'</div>'
												, 'colspan' => 3
											)


										/**
										 * @description Shift Group
										 */
										, array('data' => '<b class="big highlight">'.$this->lang->line('hris_attendance_shift_group').'</b>', 'class' => 'highlight')
										, array('data' => '<b class="big highlight">&nbsp;</b>', 'class' => 'highlight', 'colspan' => 3)

										, '<b>'.$this->lang->line('hris_attendance_shift_group').'</b>'
										, array
											(
												'data' => form_dropdown
													(
														'txtinput[33]'
														, array('' => $this->lang->line('hris_choose'))
														, ''
														, 'id="txt33" class="{validate: {required: true}}"'
													)


													. form_submit
														(
															array('id' => 'btn_save', 'class' => 'hidden')
															, $this->lang->line('button_save')
														)
												, 'colspan' => 3
											)
									);

							/**
							 * @description create the form and elements
							 */
#							$form_content = $this->table->make_columns($form_content, $this->tbl_attr['FORM']['COLUMN']);
#							$form_content = $this->table->generate($form_content);
							foreach ($form_content as $form_content_val)
							{
								$form_content_array[] = $this->table->make_columns($form_content_val, $this->tbl_attr['FORM']['COLUMN']);
							}
							foreach ($form_content_join as $form_content_join_val)
							{
								$form_content_join_array[] = $this->table->make_columns($form_content_join_val, $this->tbl_attr['FORM']['COLUMN_JOIN']);
							}

							$form_content = $this->table->generate
								(
									array_merge
										(
											$form_content_join_array[0]
											, $form_content_array[0]
											, $form_content_join_array[1]
											, $form_content_array[1]
											, $form_content_join_array[2]
											, $form_content_array[2]
											, $form_content_join_array[3]
											, $form_content_array[3]
											, $form_content_join_array[4]
										)
								);
						/**
						 * @section end form initialize
						 */
					/**
					 * @section end page init
					 */



					/**
					 * @section begin generate template variables
					 */
						/**
						 * @description css and ajax
						 */
						$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
						$this->web_data['PAGE']['JAVASCRIPT_FILES'] = $this->M_template->js_generate($js_files);

						/**
						 * @description initialize the additional javascript
						 */
						$this->web_data['JAVASCRIPT'] .= '
							<script>
								$(function()
								{
									var select_toggle = true;

									$("#btn_save").click
										(function()
										{
											js_form_action_validate
											(
												"'.$this->bz_segment['SITE_URL'].'", "ajx_", "insert", ""
												, "#frm_header", "#process", "#error_message"
												, "validate"
											);
										});


									$("#txt0").change
										(function()
										{
											$("#txt6, #txt7, #txt8, #txt9, #txt34, #txt35, #txt36").empty()
												.append( $("<option></option>").val("").html("'.$this->lang->line('hris_choose').'") );

											if ($(this).val() != "")
											{
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "company_structure/34/" + encodeURIComponent($(this).val() + ",167,0")
													, "#frm_header", "#process", "#error_message"
												);
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "job_title/6/" + $(this).val()
													, "#frm_header", "#process", "#error_message"
												);
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "company_work_location/9/" + $(this).val()
													, "#frm_header", "#process", "#error_message"
												);
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "employee_id_put", $(this).val() + "/0"
													, "#frm_header", "#process", "#error_message"
												);
												$("#txt6, #txt10").change();
											}
										});

									$("#txt1").change
										(function()
										{
											$("#txt10").empty()
												.append( $("<option></option>").val("").html("'.$this->lang->line('hris_choose').'") );

											row_to_toggle = $("table tr:eq(10) td:eq(2) b, table tr:eq(10) td:eq(3) select");
											row_to_toggle.hide();

											if ($(this).val() != "")
											{
												switch ($(this).val())
												{
													case "257":
														js_form_action
														(
															"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "instantion/10/134"
															, "#frm_header", "#process", "#error_message"
														);
														row_to_toggle.show();

													case "155":
														$("#txt2").val(256);
														$("#txt4").val("9999-12-31");
														break;

													default:
														$("#txt2").val(245).change();
														$("#txt4").val(Date.today().toString("yyyy-MM-dd"));
														break;
												}

												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "toggle_row", "42/" + $(this).val()
													, "#frm_header", "#process", "#error_message"
												);
											}
											else
											{
												row_to_toggle = $("table tr:eq(2) td:eq(2) b, table tr:eq(2) td:eq(3) select, table tr:eq(3) td:eq(2) b, table tr:eq(3) td:eq(3) input");
												row_to_toggle.hide();
											}
										});

									$("#txt2").change
										(function()
										{
											join_date_end = Date.parse($("#txt3").val()).addMonths(parseInt($("#txt2 option:selected").text())).toString("yyyy-MM-dd");
											$("#txt4").val(join_date_end);
										});

									$("#txt6, #txt36").change
										(function()
										{
											var structure_id = "";

											$("#txt7, #txt8").empty()
												.append( $("<option></option>").val("").html("'.$this->lang->line('hris_choose').'") );

											if ($("#txt34").val() != "") structure_id = $("#txt34").val();
											if ($("#txt35").val() != "") structure_id = $("#txt35").val();
											if ($("#txt36").val() != "") structure_id = $("#txt36").val();

											js_form_action
											(
												"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "company_position/7/" + encodeURIComponent($("#txt0").val() + "," + structure_id + "," + $("#txt6").val())
												, "#frm_header", "#process", "#error_message"
											);
										});

									$("#txt7").change
										(function()
										{
											$("#txt8").empty()
												.append( $("<option></option>").val("").html("'.$this->lang->line('hris_choose').'") );

											if ($(this).val() != "")
											{
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "company_structure_position_grade/8/" + $(this).val()
													, "#frm_header", "#process", "#error_message"
												);
											}
										});

									$("#txt10").change
										(function()
										{
											js_form_action
											(
												"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "employee_id_put", $("#txt0").val() + "/" + $("#txt10").val()
												, "#frm_header", "#process", "#error_message"
											);
										});


									$("#txt13, #txt14, #txt15").keyup
										(function()
										{
											$("#txt16").val( $("#txt13").val() + " " + $("#txt14").val() + " " + $("#txt15").val() );
										});

									$("#txt22").change
										(function()
										{
											select_toggle = true;
											$("#txt32").empty().append( $("<option></option>").val("") );

											if ($(this).val() != "")
											{
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "leave_management/32/" + $(this).val()
													, "#frm_header", "#process", "#error_message"
												);
											}
										});

									$("#txt34").change
										(function()
										{
											$("#txt35, #txt36").empty()
												.append( $("<option></option>").val("").html("'.$this->lang->line('hris_choose').'") );

											if ($(this).val() != "")
											{
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "company_structure/35/" + encodeURIComponent($("#txt0").val() + ",168," + $(this).val())
													, "#frm_header", "#process", "#error_message"
												);
											}
											$("#txt6").change();
										});

									$("#txt35").change
										(function()
										{
											$("#txt36").empty()
												.append( $("<option></option>").val("").html("'.$this->lang->line('hris_choose').'") );

											if ($(this).val() != "")
											{
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "company_structure/36/" + encodeURIComponent($("#txt0").val() + ",269," + $(this).val())
													, "#frm_header", "#process", "#error_message"
												);
											}
											$("#txt6").change();
										});

/**
									$("#txt36").change
										(function()
										{
											$("#txt7, #txt8").empty()
												.append( $("<option></option>").val("").html("'.$this->lang->line('hris_choose').'") );

											js_form_action
											(
												"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "company_position/7/" + encodeURIComponent($("#txt0").val() + "," + $(this).val() + ",")
												, "#frm_header", "#process", "#error_message"
											);
										});
*/

									js_form_action
									(
										"'.$this->bz_segment['SITE_URL'].'", "ajx_live_area_", "combo_put", "47/20"
										, "#frm_header", "#process", "#error_message"
									);

									js_form_action
									(
										"'.$this->bz_segment['SITE_URL'].'", "ajx_live_", "combo_put", "shift_group/33/" + $(this).val()
										, "#frm_header", "#process", "#error_message"
									);

									$(".link_select_toggle").click
										(function()
										{
											select_id = $(this).parent().prev().attr("id");
											$("#" + select_id + " option").prop("selected", select_toggle);
											select_toggle = ! select_toggle;
											return false;
										});

									js_datetime_picker_show("txt3", "txt5");
									js_datetime_picker_show("txt5", "txt6");
									js_datetime_picker_show("txt21", "txt22");
									js_datetime_picker_show("txt26", "txt27");
									js_datetime_picker_show("txt28", "txt29");

									$("#txt0").focus().select();
						';

						if ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error')
						{
							$this->web_data['JAVASCRIPT'] .= '$(".nano").nanoScroller({preventPageScrolling: true});';
						}

						$this->web_data['JAVASCRIPT'] .= '
								});
							</script>
						';

						/**
						 * @description menu descriptions
						 */
						$this->web_data['PAGE']['DETAIL'] = $this->M_menu->detail();

						/**
						 * @description icon bar
						 */
						$this->load->model('admin/M_command_bar');
						$this->web_data['PAGE']['ICON_BAR'] = $this->M_command_bar->form('TRUE', TRUE, TRUE, FALSE, FALSE, FALSE);

						/**
						 * @description form dialogs
						 */
						$this->web_data['PAGE']['FORM'] = array
							(
								'OPEN' => form_open('', array('id' => 'frm_header', 'onSubmit' => 'return false'), '')
								, 'CONTENT' => $form_content
								, 'CENTER' => 'FALSE'
							);

						/**
						 * @description notes
						 */
						$this->web_data['PAGE']['NOTES'] = array
							(
								'USE' => 'TRUE'
								, 'MESSAGE' => array
									(
										'SHORTCUTS' => $this->lang->line('notes_shortcuts')
										, 'ADDITIONAL' => $this->lang->line('notes_additional')
									)
							);
					/**
					 * @section end generate template variables
					 */
				}


				/**
				 * @description get the data for editing via ajax
				 */
				if ($this->access_rights['EDIT'] && trim($prm_key) != '')
				{
					$this->web_data['JAVASCRIPT_EXTEND'] = '
						<script>
							$(function()
							{
								js_form_action
								(
									"'.$this->bz_segment['SITE_URL'].'", "ajx_data_", "put", "'.$prm_key.'"
									, "#frm_header", "#process", "#error_message"
								);
							});
						</script>
					';
				}


				/**
				 * @description log the action!
				 */
				$this->M_log->write('db_default', sprintf($this->lang->line('log_form_access'), $this->router->class, 'form'));
			}
			else
			{
				/**
				 * @description doesn't have ADD or EDIT access? redirect to desktop
				 */
				redirect('admin/desktop');
			}



		}
		else
		{
			/**
			 * @section Browser NOT Supported
			 */
				/**
				 * @description browser not supported message error
				 */
				$css_files = array(array('layout' => 'screen'));


				/**
				 * @section begin generate template variables
				 */
					/**
					 * @description css and ajax
					 */
					$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
				/**
				 * @section end generate template variables
				 */
		}



		/**
		 * @description output template variables
		 */
		$this->bz_tlite->assign('WEB_DATA', $this->web_data);


		/**
		 * @section begin output
		 */
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['HEADER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['MENU'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_DESCRIPTION'], $this->bz_tlite_var['CACHE']['PRIVATE']['GLOBAL']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM_ICON_BAR'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_SHORTCUT_BAR'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);

		$this->bz_tlite->cache = FALSE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT_EXTEND']);

		$this->bz_tlite->cache = TRUE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER_END'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		/**
		 * @section end output
		 */
	}



/**
 * @section AJAX Action Section
 */

	/**
	 * @function ajx_insert()
	 * @description insert the data
	 */
	public function ajx_insert()
	{
		if ($this->input->is_ajax_request() && ($this->access_rights['ADD'] OR $this->access_rights['EDIT']))
		{
			/**
			 * @description load the models
			 */
			$this->load->model(array('admin/M_live_data', 'admin/hris/HRIS_M_live_data'));


			$data = array
				(
					/**
					 * @description Employee Data
					 */
					'id' => ''
					, 'name_first' => $this->input->post('txtinput[13]')
					, 'name_middle' => $this->input->post('txtinput[14]')
					, 'name_last' => $this->input->post('txtinput[15]')
					, 'name_official' => $this->input->post('txtinput[16]')
					, 'name_nick' => $this->input->post('txtinput[17]')
					, 'salutation__id' => $this->input->post('txtinput[18]')
					, 'nationality__id' => $this->input->post('txtinput[19]')
					, 'birth_place' => $this->input->post('txtinput[20]')
					, 'birth_date' => $this->input->post('txtinput[21]')
					, 'gender__id' => $this->input->post('txtinput[22]')
					, 'religion__id' => $this->input->post('txtinput[23]')
					, 'marital_status__id' => $this->input->post('txtinput[24]')
					, 'marital_place' => $this->input->post('txtinput[25]')
					, 'marital_date' => $this->input->post('txtinput[26]')
					, 'id_card_number' => $this->input->post('txtinput[27]')
					, 'id_card_expire' => $this->input->post('txtinput[28]')
					, 'tax_id' => $this->input->post('txtinput[29]')
					, 'tax_type__id' => $this->input->post('txtinput[30]')
					, 'finger_id' => $this->input->post('txtinput[31]')
				);

			$return_value = $this->M_live_data->insert
				(
					'db_default'
					, $this->tbl_attr['TABLE']['NAME']
					, array('id' => $this->input->post('txtinput[11]'))
					, $data
					, 'form'
					, FALSE, ''
					, FALSE, FALSE
				);


			/**
			 * @description get the latest Employee ID
			 */
			$this->db_default->select('`fn_hris_employee_id_get_last`() as employee_id', FALSE);
			$query = $this->db_default->get();
			$header['ID'] = $query->row()->employee_id;

			/**
			 * @description process the employee company
			 */
			$data = array
				(
					/**
					 * @description Company
					 */
					'id' => ''
					, 'data__id' => $header['ID']
					, 'number' => ''
					, 'number_old' => NULL
					, 'company__id' => $this->input->post('txtinput[0]')
					, 'job_status__id' => $this->input->post('txtinput[1]')
					, 'probation_period__id' => $this->input->post('txtinput[2]')
					, 'join_date_start' => $this->input->post('txtinput[3]')
					, 'join_date_end' => $this->input->post('txtinput[4]')
					, 'effective_date' => $this->input->post('txtinput[5]')
					, 'job_title__id' => $this->input->post('txtinput[6]')
					, 'structure__id' => $this->input->post('txtinput[7]')
					, 'position_grade__id' => $this->input->post('txtinput[8]')
					, 'work_location__id' => $this->input->post('txtinput[9]')
					, 'instantion__id' => trim($this->input->post('txtinput[10]')) == '' ? NULL : $this->input->post('txtinput[10]')
				);

			$this->M_live_data->insert
				(
					'db_default'
					, 'hris_employee_company'
					, array('data__id' => $header['ID'])
					, $data
					, 'form'
					, FALSE, ''
					, FALSE, FALSE
				);

			/**
			 * @description process the leave management
			 * @status DEPRECATED! already using database trigger to auto insert into employee_leave_management
			 */
			/**
			$data = array();
			foreach ($this->input->post('txtinput[32]') as $leave_management_id)
			{
				$data[] = array
					(
						'data__id' => $header['ID']
						, 'leave_management__id' => $leave_management_id
					);
			}

			$this->HRIS_M_live_data->combo_data_set
				(
					'db_default'
					, array
						(
							'NAME' => 'hris_employee_leave_management'
							, 'WHERE' => array('data__id' => $header['ID'])
						)
					, $data
				);
			*/

			/**
			 * @description process the shift group
			 */
			$this->M_live_data->insert
				(
					'db_default'
					, 'hris_employee_shift_group'
					, array('data__id' => $header['ID'])
					, array
						(
							'data__id' => $header['ID']
							, 'shift_group__id' => $this->input->post('txtinput[33]')
							, 'activated' => 'Yes'
						)
					, 'form'
					, FALSE, ''
					, FALSE, FALSE
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}



/**
 * @section AJAX Live Section
 */

	/**
	 * @function ajx_live_area_combo_put()
	 * @description get the data and put them in combobox
	 */
	public function ajx_live_area_combo_put($prm_area_type_id = '', $prm_object_start = '0', $prm_selected_value = '')
	{
		if ($this->input->is_ajax_request() && trim($prm_area_type_id) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/hris/HRIS_M_live_data');


			$return_value = $this->HRIS_M_live_data->combo_fill('db_default', 'hris_master_area', $prm_object_start, 'NORMAL', array('area_type__id' => $prm_area_type_id), TRUE, $prm_selected_value);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_live_combo_put
	 * @description get the data and put them in combobox
	 */
	public function ajx_live_combo_put($prm_module_table = 'job_title', $prm_object_start = '0', $prm_where_key = '')
	{
		if ($this->input->is_ajax_request())
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/hris/HRIS_M_live_data');


			$table['TYPE'] = 'NORMAL';
			$table['CHOOSE_SHOW'] = TRUE;
			$table['QUERY_SPECIAL'] = FALSE;
			switch ($prm_module_table)
			{
				case 'instantion':
					$table['NAME'] = 'hris_master_instantion';
					$table['WHERE'] = array('instantion_type__id' => $prm_where_key);
					break;

				case 'job_title':
					$table['NAME'] = 'hris_organization_company_job_title';
					$table['WHERE'] = array('company__id' => $prm_where_key);
					break;

				case 'company_structure':
					$table['NAME'] = 'hris_organization_company_structure';

					$prm_where_key = explode('%2C', $prm_where_key);
					$table['WHERE'] = array('company__id' => $prm_where_key[0], 'structure_level__id' => $prm_where_key[1], 'parent_id' => $prm_where_key[2], 'status' => 'Active');
					break;

				case 'company_position':
					$prm_where_key = explode('%2C', $prm_where_key);

					$return_value = array
						(
							'js_action_combo_item_add'
							, array('' => $this->lang->line('hris_choose'))
								+ $this->M_functions->array_flatten_with_key($this->HRIS_M_live_data->company_structure_position('db_default', $prm_where_key[0], $prm_where_key[1], $prm_where_key[2]))
							, $prm_object_start
						);


#					$table['NAME'] = 'company_structure';
#					$table['TYPE'] = 'RECURSIVE';
#					$prm_where_key = explode('%2C', $prm_where_key);
#					$table['WHERE'] = array
#						(
#							'company__idx' => $prm_where_key[0]
#							, 'structure_level__id' => 169
#							, 'status' => '"Active"'
#							, 'id in ' => '(select structure__id from bz_hris_organization_company_structure_position where job_title__id = '.$prm_where_key[1].')'
#						);
#					$table['QUERY_SPECIAL'] = TRUE;
					break;

				case 'company_structure_position_grade':
					$table['NAME'] = 'hris_organization_company_structure_position_grade';
					$table['WHERE'] = array('structure__id' => $prm_where_key);
					break;

				case 'company_work_location':
					$table['TYPE'] = 'RECURSIVE';
					$table['NAME'] = 'company_work_location';
					$table['WHERE'] = array('company__id' => $prm_where_key);
					break;

				case 'shift_group':
					$table['NAME'] = 'hris_attendance_shift_group';
					$table['WHERE'] = array();
					break;

				case 'leave_management':
					$table['CHOOSE_SHOW'] = FALSE;
					$table['NAME'] = 'hris_attendance_leave_management';
					$table['WHERE'] = 'gender__id = 221 or gender__id = "'.$prm_where_key.'"';
					break;
			}


			if ( ! isset($return_value))
			{
				$return_value = $this->HRIS_M_live_data->combo_fill('db_default', $table['NAME'], $prm_object_start, $table['TYPE'], $table['WHERE'], FALSE, '', $table['CHOOSE_SHOW'], $table['QUERY_SPECIAL']);
			}


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_live_toggle_row
	 * @description toggle the row to hide or show
	 */
	public function ajx_live_toggle_row($prm_module_table = '', $prm_key = '')
	{
		if ($this->input->is_ajax_request() && trim($prm_module_table) != '' && trim($prm_key) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/hris/HRIS_M_live_data');


			$fill_type_data = $this->HRIS_M_live_data->master_data_fill_type_get($prm_module_table, $prm_key);
			$return_value = array
				(
					'js_action_run'
					, array
						(
							'row_to_toggle = $("table tr:eq(2) td:eq(2) b, table tr:eq(2) td:eq(3) select, table tr:eq(3) td:eq(2) b, table tr:eq(3) td:eq(3) input");'
							. ( ($fill_type_data[1] == 'Date') ? 'row_to_toggle.show();' : 'row_to_toggle.hide();' )
						)
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_live_employee_id_put
	 * @description put the auto generated employee ID & Number :)
	 */
	public function ajx_live_employee_id_put($prm_key = '1', $prm_company_outsource_id = '0', $prm_object_start = 11)
	{
		if ($this->input->is_ajax_request())
		{
			($prm_company_outsource_id != '0') ? $company_type = 'OUTSOURCE' : $company_type = 'NORMAL';

			$this->db_default->select('`fn_hris_employee_id_generate`() as employee_id, `fn_hris_employee_number_generate`("'.$company_type.'", "'.$prm_key.'", '.$prm_company_outsource_id.') as employee_number', FALSE);
			$query = $this->db_default->get();

			$return_value = array
				(
					'js_action_data'
					, array($query->row()->employee_id, $query->row()->employee_number)
					, $prm_object_start
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}



}

/** End of file add_new.php */
/** Location: ./biskitz_mpc/biskitz/controllers/admin/hris/employee/add_new.php */
